package com.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.entity.Student;
import com.ssm.entity.StudentPojo;
import com.ssm.service.DeskService;
import com.ssm.service.IChairService;
import com.ssm.service.RlteastuService;
import com.ssm.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private IChairService chairService;
    @Autowired
    private DeskService deskService;
    @Autowired
    private RlteastuService rlteastuService;

    @RequestMapping("/getAllStudent")
    @ResponseBody
    public Object getAllStudent(Integer page,Integer limit){
        PageHelper.startPage(page, limit);
        List<Student> students = studentService.selectAll();
        PageInfo pageInfo = new PageInfo(students);
        Map<String, Object> tableData = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        tableData.put("code", 0);
        tableData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", pageInfo.getTotal());
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", pageInfo.getList());

        return tableData;
    }

    @RequestMapping("/selectStudent")
    @ResponseBody
    public Object selectStudent(Integer page, Integer limit, StudentPojo studentPojo){
        PageHelper.startPage(page, limit);
        List<Student> students = studentService.selectStudent(studentPojo);
        PageInfo pageInfo = new PageInfo(students);
        Map<String, Object> tableData = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        tableData.put("code", 0);
        tableData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", pageInfo.getTotal());
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", pageInfo.getList());

        return tableData;
    }

    @RequestMapping("updateByPrimaryKey")
    @ResponseBody
    public String updateByPrimaryKey(Student student) {
        int i = studentService.updateByPrimaryKey(student);
        if (i == 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    @RequestMapping("insert")
    @ResponseBody
    public String insert(Student student) {
        int i = studentService.insert(student);
        if (i == 1) {
            return "增加成功";
        } else {
            return "增加失败";
        }
    }

    /**
     * 删除多个学生，需要将关联的外键全部删除
     * 将学生对应的椅子（S_ID）置空
     * 将学生对应的桌子（S_ID）置空
     * 将学生对应的教师删除
     */
    @RequestMapping("deleteAll")
    @ResponseBody
    public String deleteAll(String studentId){
        List<String> list = new ArrayList<String>();
        String[] strs = studentId.split(",");
        for (String s : strs){
            list.add(s);
        }

        //学生对应的椅子（S_ID）置空
        int chairResult = chairService.updateChairSidToDeleteStudent(list);
        //将学生对应的桌子（S_ID）置空
        int deskResult = deskService.updateDeskSidToDeleteStudent(list);
        //将学生对应的教师删除
        int teacherResult = rlteastuService.deleteRlteastuSidToDeleteStudent(list);
        //删除多个学生
        int studentResult = studentService.deleteAll(list);

        if (studentResult>0) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

    @RequestMapping("falseDelete")
    @ResponseBody
    public String falseDelete(Integer sid) {
        int i = studentService.falseDeleteIsUpdateState(sid);
        if (i == 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

}
